package character.sprite.model;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import session.SharableObject;

import character.sprite.data.CharacterStyleDatabase;
import character.sprite.gui.CharacterSpriteAnimator;

public class CharacterSprite extends SharableObject {

	private String name, body, head, hair, dress, face;
	private BufferedImage image;
	private boolean changed;

	public CharacterSprite(String name, String body, String head, String hair,
			String dress, String face) {
		super();
		this.name = name;
		this.body = body;
		this.head = head;
		this.hair = hair;
		this.dress = dress;
		this.face = face;
		this.changed = true;
	}

	public CharacterSprite(String name) {
		super();
		this.name = name;
		this.body = "[[No Body]]";
		this.head = "[[No Head]]";
		this.hair = "[[No Hair]]";
		this.dress = "[[No Dress]]";
		this.face = "[[No Face]]";
		this.changed = true;
	}

	public synchronized String getName() {
		return name;
	}

	public synchronized BufferedImage getThumbnailImage() {
		return this.getFullImage().getSubimage(0,
				CharacterSpriteAnimator.SPRITE_HEIGHT * 2,
				CharacterSpriteAnimator.SPRITE_WIDTH,
				CharacterSpriteAnimator.SPRITE_HEIGHT);
	}

	public synchronized BufferedImage getFullImage() {
		if (changed) {
			CharacterStyle style = CharacterStyleDatabase.getInstance()
					.getStyle(body);
			BufferedImage bodyImage = null;
			if (style != null)
				bodyImage = style.getFullImage();
			style = CharacterStyleDatabase.getInstance().getStyle(hair);
			BufferedImage hairImage = null;
			if (style != null)
				hairImage = style.getFullImage();
			style = CharacterStyleDatabase.getInstance().getStyle(head);
			BufferedImage headImage = null;
			if (style != null)
				headImage = style.getFullImage();
			style = CharacterStyleDatabase.getInstance().getStyle(face);
			BufferedImage faceImage = null;
			if (style != null)
				faceImage = style.getFullImage();
			style = CharacterStyleDatabase.getInstance().getStyle(dress);
			BufferedImage dressImage = null;
			if (style != null)
				dressImage = style.getFullImage();
			image = new BufferedImage(bodyImage.getWidth(), bodyImage
					.getHeight(), BufferedImage.TYPE_INT_ARGB);
			Graphics2D g2d = (Graphics2D) image.getGraphics();
			if (bodyImage != null)
				g2d.drawImage(bodyImage, 0, 0, null);
			if (faceImage != null)
				g2d.drawImage(faceImage, 0, -6, null);
			if (dressImage != null)
				g2d.drawImage(dressImage, 0, 0, null);
			if (hairImage != null)
				g2d.drawImage(hairImage, 0, 0, null);
			if (headImage != null)
				g2d.drawImage(headImage, 0, 0, null);
			this.changed = false;
		}
		return image;
	}

	public synchronized String getBody() {
		return body;
	}

	public synchronized void setBody(String body) {
		this.body = body;
		this.changed = true;
	}

	public synchronized String getHead() {
		return head;
	}

	public synchronized void setHead(String head) {
		this.head = head;
		this.changed = true;
	}

	public synchronized String getHair() {
		return hair;
	}

	public synchronized void setHair(String hair) {
		this.hair = hair;
		this.changed = true;
	}

	public synchronized String getDress() {
		return dress;
	}

	public synchronized void setDress(String dress) {
		this.dress = dress;
		this.changed = true;
	}

	public synchronized String getFace() {
		return face;
	}

	public synchronized void setFace(String face) {
		this.face = face;
		this.changed = true;
	}

	public synchronized void setName(String name) {
		this.name = name;
		this.changed = true;
	}

}
